using System;
using System.Collections.Generic;
using System.Threading;

namespace csvviewer.logic
{
	public class ConcurrentList<T>
	{
		private List<T> _list;
		private ReaderWriterLock _rwl;
		
		public ConcurrentList ()
		{
			_list = new List<T>();
			_rwl = new ReaderWriterLock();
		}
		
		public void Add(T item)
		{
			_rwl.AcquireWriterLock(500);
			try
			{
				_list.Add(item);
			}
			finally
			{
				_rwl.ReleaseWriterLock();
			}
		}
		
		public int Count
		{
			get {
				_rwl.AcquireReaderLock(500);
				try
				{
					return _list.Count;
				}
				finally
				{
					_rwl.ReleaseReaderLock();
				}
			}
		}
		
		public T this[int index]
		{
			get {
				_rwl.AcquireReaderLock(500);
				try
				{
					return _list[index];
				}
				finally
				{
					_rwl.ReleaseReaderLock();
				}				
			}
		}
		
		public IEnumerable<T> GetRange(int index, int count)
		{
			_rwl.AcquireReaderLock(500);
			try
			{
				return _list.GetRange(index, count);
			}
			finally
			{
				_rwl.ReleaseReaderLock();
			}		
		}
	}
}